package com.pd.backend.service.impl;

import com.pd.backend.dtos.LoginDto;
import com.pd.backend.vo.LoginVo;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.DGlobalUserMapper;
import com.pd.backend.pojo.DGlobalUser;
import com.pd.backend.service.UserService;
import com.pd.backend.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * 管理登录业务层实现类
 * @Auther : 菜鸡编程
 * @Date:2023/1/10
 * @Description:
 * @VERSON:1.8
 */
@Service
@Slf4j
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private DGlobalUserMapper dGlobalUserMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 管理登录
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult userLogin(LoginDto loginDto) {
        //校验参数
        //判断请求参数是否存在
        if (Objects.isNull(loginDto)) {
            //请求参数为空,返回错误信息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断账号和密码是否为空
        String account = loginDto.getAccount();
        String password = loginDto.getPassword();

        if (StringUtils.isBlank(account)&&StringUtils.isBlank(password)&&StringUtils.isBlank(loginDto.getAuthCode())) {
            //账号密码或者验证码为空,返回错误信息:缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //根据账号查询管理对象
        DGlobalUser dGlobalUser = dGlobalUserMapper.selectUser(account);
        //判断管理对象是否存在
        if (Objects.isNull(dGlobalUser)) {
            //不存在,返回错误消息:用户信息不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //比对密码
        String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!pwdMd5.equals(dGlobalUser.getPassword())) {
            //密码不正确,返回错误消息:密码错误
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //比对验证码
        //获取验证码
        //创建redis的key
        String key = RedisUtils.createKey("app_login_code", loginDto.getAccount());
        //获取redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(key);
        //查询验证码是否一致
        //判断验证码是否为空
        if (StringUtils.isBlank(redisCode)) {
            //验证码为空,返回错误消息:验证码异常
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_CODE_EX);
        }
        //判断验证码是否一致
        if (!loginDto.getAuthCode().equals(redisCode)) {
            //验证码不一致,返回错误消息:验证码错误
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_CODE_ERROR);
        }
        //验证码正确,登录成功
        //封装数据
        LoginVo loginResult = new LoginVo();
        BeanUtils.copyProperties(dGlobalUser,loginResult);
        //登录成功
        return ResponseResult.okResult(loginResult);
    }
}
